<template>
  <div class="home gradient-bg">
    <div class="hero glass">
      <h1 class="title flowing-text">声声不息</h1>
      <div class="hero-actions">
        <!-- 设置按钮 -->
        <button class="primary-btn" @click="openSettings">设置</button>
        <!-- 模板按钮 -->
        <button class="primary-btn" @click="goTemplate">模板</button>
      </div>
    </div>

    <div class="center-wrap">
      <div class="intro glass">
        <h1 class="intro-title">小息在此！你的电脑，“说” 了算</h1>
        <p class="intro-desc">想听歌不用找图标，想写文章不用敲键盘？小息，就是你的 “语音小管家”。只要跟小息说一声，都能帮你搞定。</p>
      </div>
    </div>

    <!-- 设置弹窗（模态） -->
    <div v-if="showSettings" class="modal-overlay" @click.self="closeSettings">
      <div class="modal glass">
        <h2>设置工作目录</h2>
        <p class="desc">请选择仅包含目录的路径列表，用于文件搜索。</p>

        <div class="selected-list" v-if="selectedDirs && selectedDirs.length">
          <div class="dir-item" v-for="(d, i) in selectedDirs" :key="i">{{ d }}</div>
        </div>
        <div v-else class="empty">当前未选择目录</div>

        <div class="actions">
          <button class="pick-btn" @click="pickDirectories">选择文件夹</button>
          <button class="save-btn" @click="saveSettings">保存</button>
          <button class="cancel-btn" @click="closeSettings">取消</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'HomeView',
  data() {
    return {
      showSettings: false,
      selectedDirs: [],
    }
  },
  methods: {
    openSettings() {
      this.showSettings = true
      // 初始化已保存目录（可视化）
      if (window.electronSettingsAPI && typeof window.electronSettingsAPI.getWorkDirs === 'function') {
        window.electronSettingsAPI.getWorkDirs().then((dirs) => {
          if (Array.isArray(dirs)) this.selectedDirs = dirs
        }).catch(() => { })
      }
    },
    goTemplate() {
      this.$router.push('/template')
    },
    closeSettings() {
      this.showSettings = false
    },
    async pickDirectories() {
      try {
        if (!window.electronSettingsAPI || typeof window.electronSettingsAPI.selectDirectories !== 'function') {
          alert('目录选择器不可用')
          return
        }
        const dirs = await window.electronSettingsAPI.selectDirectories()
        if (Array.isArray(dirs)) {
          this.selectedDirs = dirs
        }
      } catch (err) {
        console.error('选择目录失败:', err)
        alert('选择目录失败，请重试')
      }
    },
    async saveSettings() {
      try {
        if (!window.electronSettingsAPI || typeof window.electronSettingsAPI.setWorkDirs !== 'function') {
          alert('保存接口不可用')
          return
        }
        const res = await window.electronSettingsAPI.setWorkDirs(this.selectedDirs)
        if (res && res.success) {
          alert('保存成功')
          this.showSettings = false
        } else {
          alert(res && res.message ? res.message : '保存失败')
        }
      } catch (err) {
        console.error('保存设置失败:', err)
        alert('保存失败，请重试')
      }
    }
  }
}
</script>

<style scoped>
.home {
  padding: 20px;
  text-align: center;
  box-sizing: border-box;
}

.nav-links {
  margin-top: 30px;
}

.nav-link {
  display: inline-block;
  margin: 10px;
  padding: 10px 20px;
  background: #007bff;
  color: white;
  text-decoration: none;
  border-radius: 5px;
}

.nav-link:hover {
  background: #0056b3;
}

.settings-btn {
  margin-top: 20px;
  padding: 8px 16px;
  background: #28a745;
  color: #fff;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.settings-btn:hover {
  background: #218838;
}

.template-btn {
  margin-left: 12px;
  padding: 8px 16px;
  background: #6f42c1;
  color: #fff;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.template-btn:hover {
  background: #5936a2;
}

.flowing-text {
  /* 设置渐变背景 */
  background: linear-gradient(90deg, #ff6ec7 , #fff59d,  #cc66ff, #6ee7ff, #ff6ec7);
  background-size: 400% 100%;
  
  /* 背景裁剪到文字 */
  -webkit-background-clip: text;
  background-clip: text;
  
  /* 文字颜色透明以显示背景 */
  color: transparent;
  
  /* 动画效果 */
  animation: flowing-color 3s linear infinite;
}

@keyframes flowing-color {
  0% {
    background-position: 0% 50%;
  }
  100% {
    background-position: 100% 50%;
  }
}

/* 模态弹窗 */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  /* 高于页面其他内容 */
}

.modal {
  width: 520px;
  max-width: 90vw;
  background: #fff;
  border-radius: 8px;
  padding: 16px 20px;
  box-shadow: 0 12px 24px rgba(0, 0, 0, 0.2);
}

.modal h2 {
  margin: 0 0 8px;
}

.desc {
  color: #666;
  margin-bottom: 12px;
}

.selected-list {
  max-height: 200px;
  overflow: auto;
  text-align: left;
}

.dir-item {
  padding: 6px 8px;
  border-bottom: 1px solid #eee;
  font-size: 13px;
}

.empty {
  color: #999;
  margin: 8px 0;
}

.actions {
  display: flex;
  gap: 10px;
  margin-top: 14px;
  justify-content: flex-end;
}

.pick-btn {
  padding: 8px 14px;
  background: #17a2b8;
  color: #fff;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.pick-btn:hover {
  background: #138496;
}

.save-btn {
  padding: 8px 14px;
  background: #007bff;
  color: #fff;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.save-btn:hover {
  background: #0069d9;
}

.cancel-btn {
  padding: 8px 14px;
  background: #6c757d;
  color: #fff;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.cancel-btn:hover {
  background: #5a6268;
}

/* 背景渐变，与 TemplateView 保持一致 */
.gradient-bg {
  min-height: 100vh;
  background: radial-gradient(circle at 20% 10%, #ff6ec7 0%, transparent 40%),
    radial-gradient(circle at 80% 20%, #6ee7ff 0%, transparent 45%),
    radial-gradient(circle at 30% 80%, #fff59d 0%, transparent 50%),
    linear-gradient(135deg, #c3dafe 0%, #fbcfe8 100%);
}

/* 玻璃态卡片 */
.glass {
  background: rgba(255, 255, 255, 0.35);
  border: 1px solid rgba(255, 255, 255, 0.4);
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.15);
  backdrop-filter: blur(12px);
  border-radius: 16px;
}

/* 顶部标题及按钮区域 */
.title {
  margin: 0;
  font-weight: 700;
  letter-spacing: .5px;
}

.hero {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  margin: 0 20px 16px;
}

.hero-actions {
  display: flex;
  align-items: center;
  gap: 12px;
}

/* 一致风格按钮（与 TemplateView 的 primary 相同） */
.primary-btn {
  background: #6f42c1;
  color: #fff;
  border: none;
  padding: 8px 14px;
  border-radius: 10px;
  box-shadow: 0 10px 20px rgba(111, 66, 193, .25);
  cursor: pointer;
}

.primary-btn:hover {
  filter: brightness(1.05);
}

/* 调整模态遮罩透明度更柔和 */
.modal-overlay {
  background: rgba(0, 0, 0, 0.25);
}

/* modal采用玻璃态：通过类组合覆盖背景等 */
.modal {
  padding: 16px 20px;
}

/* 中心介绍块 */
.center-wrap {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 60vh;
  padding: 20px;
}
.intro {
  max-width: 840px;
  text-align: center;
  padding: 26px 30px;
}
.intro-title {
  margin: 0 0 12px;
  font-size: 32px;
  font-weight: 800;
  letter-spacing: .3px;
  line-height: 1.25;
  color: #373333;
}
.intro-desc {
  margin: 0;
  font-size: 16px;
  color: #333;
  line-height: 1.7;
}
</style>